package com.deliinsns.app.basedata;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.deliinsns.app.basedata.bean.BaseData;
import com.deliinsns.sys.BasicController;
import com.deliinsns.sys.Messenger;
import com.deliinsns.sys.util.SystemConstant;
@Controller
public class BaseDataController extends BasicController {
	
	 @Autowired  
     private  BaseDataDao baseDataDao;  
	 
	@RequestMapping(value = "/basedata/jobclassify", method = RequestMethod.GET)
	 public void getJobclassify(HttpServletRequest request,HttpServletResponse response)throws Exception {
		Messenger msg= new Messenger();
		ServletContext context=request.getSession().getServletContext();
		List<BaseData> list=(List<BaseData>) context.getAttribute("jobclassify");
		if(CollectionUtils.isEmpty(list)){ //如果application里面没有数据，则从数据库去获取
			list= baseDataDao.findByHql("from BaseData b where b.dataType='jobclassify'");
			context.setAttribute("jobclassify",list);
		}
		msg.setState(SystemConstant.STATE_SUCCESS);
		msg.setItems(list);
		msg.setMsgCode(SystemConstant.GET_SUCCESS);
		out(request,response,msg);
	 }
	 
	@RequestMapping(value = "/basedata/jobclassifyAndarea", method = RequestMethod.GET)
	 public void getJobclassifyAndarea(HttpServletRequest request,HttpServletResponse response)throws Exception {
		JSONObject json = new JSONObject();
		List<BaseData> classifyList= null,areaList= null,currentCity=null;
		classifyList= baseDataDao.findByHql("from BaseData b where b.dataType='jobclassify' and b.parent=0");
		String nativeCity= "shanghai";
		currentCity= baseDataDao.findByHql("from BaseData b where b.dataType='area' and b.key='"+nativeCity+"'");
		if(CollectionUtils.isNotEmpty(currentCity)){
			BaseData temp= currentCity.get(0);
			areaList= baseDataDao.findByHql("from BaseData b where b.dataType='area' and b.parent='"+temp.getId()+"'");
		}
		json.put("classifyList", classifyList);
		json.put("areaList", areaList);
		json.put("state", SystemConstant.STATE_SUCCESS);
		out(request,response,json);
	 }
	@RequestMapping(value = "/basedata/areaofcity", method = RequestMethod.GET)
	 public void getAreaOfCity(HttpServletRequest request,HttpServletResponse response)throws Exception {
		JSONObject json = new JSONObject();
		List<BaseData> areaList= null,currentCity=null;
		String nativeCity= "shanghai";
		currentCity= baseDataDao.findByHql("from BaseData b where b.dataType='area' and b.key='"+nativeCity+"'");
		if(CollectionUtils.isNotEmpty(currentCity)){
			BaseData temp= currentCity.get(0);
			areaList= baseDataDao.findByHql("from BaseData b where b.dataType='area' and b.parent='"+temp.getId()+"'");
		}
		json.put("areaList", areaList);
		json.put("state", SystemConstant.STATE_SUCCESS);
		out(request,response,json);
	 }
	

}
